.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Copyright (c) 2004 Sun Microsystems, Inc.  All Rights Reserved.
.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
.\" Copyright (c) 2014, Joyent, Inc.
.\"
.TH MKNOD 2 "Feb 19, 2004"
.SH NAME
mknod, mknodat \- make a directory, a special file, or a regular file
.SH SYNOPSIS
.LP
.nf
#include <sys/stat.h>

\fBint\fR \fBmknod\fR(\fBconst char *\fR\fIpath\fR, \fBmode_t\fR \fImode\fR, \fBdev_t\fR \fIdev\fR);
.fi
.LP
.nf
\fBint\fR \fBmknodat(\fBint\fR \fIfd\fR, \fBconst char *\fR\fIpath\fR, \fBmode_t\fR \fImode\fR, \fBdev_t\fR \fIdev\fR);
.fi

.SH DESCRIPTION
.LP
The \fBmknod()\fR and \fBmknodat()\fR function creates a new file named by the
path name pointed to by \fIpath\fR. The file type and permissions of the new
file are initialized from \fImode\fR.
.sp
.LP
The file type is specified in \fImode\fR by the \fBS_IFMT\fR bits, which must
be set to one of the following values:
.sp
.ne 2
.na
\fB\fBS_IFIFO\fR\fR
.ad
.RS 11n
fifo special
.RE

.sp
.ne 2
.na
\fB\fBS_IFCHR\fR\fR
.ad
.RS 11n
character special
.RE

.sp
.ne 2
.na
\fB\fBS_IFDIR\fR\fR
.ad
.RS 11n
directory
.RE

.sp
.ne 2
.na
\fB\fBS_IFBLK\fR\fR
.ad
.RS 11n
block special
.RE

.sp
.ne 2
.na
\fB\fBS_IFREG\fR\fR
.ad
.RS 11n
ordinary file
.RE

.sp
.LP
The file access permissions are specified in \fImode\fR by the 0007777 bits,
and may be constructed by a bitwise \fBOR\fR operation of the following values:
.sp

.sp
.TS
l l l
l l l .
\fBS_ISUID\fR	04000	Set user ID on execution.
\fBS_ISGID\fR	020#0	T{
Set group ID on execution if # is \fB7\fR, \fB5\fR, \fB3\fR, or \fB1\fR. Enable mandatory file/record locking if # is \fB6\fR, \fB4\fR, \fB2\fR, or \fB0\fR
T}
\fBS_ISVTX\fR	01000	T{
On directories, restricted deletion flag; on regular files on a UFS file system, do not cache flag.
T}
\fBS_IRWXU\fR	00700	Read, write, execute by owner.
\fBS_IRUSR\fR	00400	Read by owner.
\fBS_IWUSR\fR	00200	Write by owner.
\fBS_IXUSR\fR	00100	T{
Execute (search if a directory) by owner.
T}
\fBS_IRWXG\fR	00070	Read, write, execute by group.
\fBS_IRGRP\fR	00040	Read by group.
\fBS_IWGRP\fR	00020	Write by group.
\fBS_IXGRP\fR	00010	Execute by group.
\fBS_IRWXO\fR	00007	Read, write, execute (search) by others.
\fBS_IROTH\fR	00004	Read by others.
\fBS_IWOTH\fR	00002	Write by others
\fBS_IXOTH\fR	00001	Execute by others.
.TE

.sp
.LP
The owner \fBID\fR of the file is set to the effective user \fBID\fR of the
process. The group \fBID\fR of the file is set to the effective group \fBID\fR
of the process.  However, if the \fBS_ISGID\fR bit is set in the parent
directory, then the group \fBID\fR of the file is inherited from the parent.
If the group \fBID\fR of the new file does not match the effective group
\fBID\fR or one of the supplementary group IDs, the \fBS_ISGID\fR bit is
cleared.
.sp
.LP
The access permission bits of \fImode\fR are modified by the process's file
mode creation mask: all bits set in the process's file mode creation mask are
cleared (see \fBumask\fR(2)). If \fImode\fR indicates a block or character
special file, \fIdev\fR is a configuration-dependent specification of a
character or block I/O device. If \fImode\fR does not indicate a block special
or character special device, \fIdev\fR is ignored. See \fBmakedev\fR(3C).
.sp
.LP
If \fIpath\fR is a symbolic link, it is not followed.
.sp
.LP
The \fBmknodat()\fR function is similar to \fBmknod()\fR; however, when
\fIpath\fR is a relative path, it is resolved starting at the directory
represented by the file descriptor \fIfd\fR. To start at the current working
directory, \fIfd\fR may be set to the special value \fBAT_FDCWD\fR.
.SH RETURN VALUES
.LP
Upon successful completion, \fBmknod()\fR and \fBmknodat()\fR return \fB0\fR.
Otherwise, they return \fB\(mi1\fR, the new file is not created, and \fBerrno\fR
is set to indicate the error.
.SH ERRORS
.LP
The \fBmknod()\fR and \fBmknodat()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 16n
A component of the path prefix denies search permission, or write permission is
denied on the parent directory.
.RE

.sp
.ne 2
.na
\fB\fBEDQUOT\fR\fR
.ad
.RS 16n
The directory where the new file entry is being placed cannot be extended
because the user's quota of disk blocks on that file system has been exhausted,
or the user's quota of inodes on the file system where the file is being
created has been exhausted.
.RE

.sp
.ne 2
.na
\fB\fBEEXIST\fR\fR
.ad
.RS 16n
The named file exists.
.RE

.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 16n
The \fIpath\fR argument points to an illegal address.
.RE

.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 16n
A signal was caught during the execution of the \fBmknod()\fR function.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
An invalid argument exists.
.RE

.sp
.ne 2
.na
\fB\fBEIO\fR\fR
.ad
.RS 16n
An I/O error occurred while accessing the file system.
.RE

.sp
.ne 2
.na
\fB\fBELOOP\fR\fR
.ad
.RS 16n
Too many symbolic links were encountered in translating \fIpath\fR.
.RE

.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
The length of the \fIpath\fR argument exceeds {\fB\fR\fBPATH_MAX\fR}, or the
length of a \fIpath\fR component exceeds {\fB\fR\fBNAME_MAX\fR} while
\fB\fR\fB_POSIX_NO_TRUNC\fR is in effect.
.RE

.sp
.ne 2
.na
\fB\fBENOENT\fR\fR
.ad
.RS 16n
A component of the path prefix specified by \fIpath\fR does not name an
existing directory or \fIpath\fR is an empty string.
.RE

.sp
.ne 2
.na
\fB\fBENOLINK\fR\fR
.ad
.RS 16n
The \fIpath\fR argument points to a remote machine and the link to that machine
is no longer active.
.RE

.sp
.ne 2
.na
\fB\fBENOSPC\fR\fR
.ad
.RS 16n
The directory that would contain the new file cannot be extended or the file
system is out of file allocation resources.
.RE

.sp
.ne 2
.na
\fB\fBENOTDIR\fR\fR
.ad
.RS 16n
A component of the path prefix is not a directory. In addition, when calling
\fBmknodat()\fR, if \fIpath\fR is a relative path and \fIfd\fR is a valid file
descriptor which does not refer to a directory.
.RE

.sp
.ne 2
.na
\fB\fBEPERM\fR\fR
.ad
.RS 16n
Not all privileges are asserted in the effective set of the calling process.
.RE

.sp
.ne 2
.na
\fB\fBEROFS\fR\fR
.ad
.RS 16n
The directory in which the file is to be created is located on a read-only file
system.
.RE

.sp
.LP
The \fBmknodat()\fR function will fail if:
.sp
.ne 2
.na
.B EBADF
.ad
.RS 16n
The \fIpath\fR argument is a relative path and \fIfd\fR is not a valid open file
descriptor or the special value \fBAT_FDCWD\fR.
.RE

.sp
.LP
The \fBmknod()\fR function may fail if:
.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
Pathname resolution of a symbolic link produced an intermediate result whose
length exceeds {\fBPATH_MAX\fR}.
.RE

.SH USAGE
.LP
Applications should use the \fBmkdir\fR(2) function to create a directory
because appropriate permissions are not required and because \fBmknod()\fR
might not establish directory entries for the directory itself (\fB\&.\fR) and
the parent directory (\fB\&.\|.\fR). The \fBmknod()\fR function can be invoked
only by a privileged user for file types other than FIFO special. The
\fBmkfifo\fR(3C) function should be used to create FIFOs.
.sp
.LP
Doors are created using \fBdoor_create\fR(3C) and can be attached to the file
system using \fBfattach\fR(3C). Symbolic links can be created using
\fBsymlink\fR(2). An endpoint for communication can be created using
\fBsocket\fR(3SOCKET).
.SH ATTRIBUTES
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Standard
_
MT-Level	Async-Signal-Safe
.TE

.SH SEE ALSO
.LP
.BR chmod (2),
.BR creat (2),
.BR exec (2),
.BR mkdir (2),
.BR open (2),
.BR stat (2),
.BR symlink (2),
.BR umask (2),
.BR door_create (3C),
.BR fattach (3C),
.BR makedev (3C),
.BR mkfifo (3C),
.BR stat.h (3HEAD),
.BR socket (3SOCKET),
.BR attributes (7),
.BR privileges (7),
.BR standards (7)
